package cn.com.surker.workcon.service.zhcl;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.control.entity.ControlInvoice;
import cn.com.surker.enums.production.invoice.ControlProStatusEnum;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.production.dao.IndentDispatchDao;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProductService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 珠海长路工控机的生产数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProductServiceImpl extends GKJDBCService implements ProductService {

	@Resource
	private IndentDispatchDao indentDispatchDao;

	/**
	 * 根据erp的派单信息,查询派车数据
	 *
	 * @param dispatchId erp的派单id
	 * @return
	 */
	public ProductionIndentDispatch queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		ProductionIndentDispatch dispatch = indentDispatchDao.selectById(dispatchId);
		if (dispatch == null) {
			return null;
		}
		//	查询工控数据
		String recordTableName = ZhclUtil.getRecordTableName(line, "N", "B");
		String queryProSql = "select BNo,StNo,StNm,BNo,PBID,BTN,BTm,BTS,BSD,BST,PPC,RCC,COrM,KSV,KV,KPV,Optor,OBNo,ANote,MNote,RFlag,ErpNo" +
			" FROM " + recordTableName + " where OBNo = ? and RFlag = ?";

		Object[] proParam = {dispatchId, 10};
		//	查询数据
		List<Map<String, Object>> proResList = lineJdbcTemp.queryForList(queryProSql, proParam);
		if (proResList == null || proResList.isEmpty()) {
			//	没有查询到数据
			return null;
		}
		//	取出数据
		Map<String, Object> proRes = proResList.get(0);
		//	生产时间
		Date prodTim = null;
		String prodTimE = String.valueOf(proRes.get("ANote"));
		if (StringUtils.isNotBlank(prodTimE)) {
			prodTim = DateUtil.convertDate(prodTimE);
		} else {
			prodTim = new Date();
		}
		//	生产方量
		Double prodMete = Double.valueOf(String.valueOf(proRes.get("KV")));
		//	工控发货单id
		String prodId = String.valueOf(proRes.get("BNo"));
		//	工控生产状态
		String proStr = String.valueOf(proRes.get("RFlag"));
		Integer controlProStatus = Integer.valueOf(proStr);
		controlProStatus = ControlProStatusEnum.COMPLETED.getCode();
		//	查询配合比编号
		String recipe = String.valueOf(proRes.get("RCC"));
		//	生产派车数据
		ProductionIndentDispatch pid = new ProductionIndentDispatch();
		pid.setProTime(prodTim);
		pid.setProStere(prodMete);
		pid.setControlId(prodId);
		pid.setControlProStatus(controlProStatus);
		pid.setRecipe(recipe);
		return pid;
	}


	@Override
	public Boolean inProing(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		String distanceCtrlTable = ZhclUtil.getCtrlTableName(line, "DistanceCtrl");
		//	查询工控机的生产状态
		String querySql = "select ProductStatus from " + distanceCtrlTable + " where OrderID = " + dispatchId;
		//	查询数据
		List<Map<String, Object>> maps = lineTemplate.queryForList(querySql);
		if (maps == null || maps.isEmpty()) {
			return false;
		}
		Map<String, Object> map = maps.get(0);
		if (map == null) {
			return false;
		}
		Object status = map.get("ProductStatus");
		if (status == null) {
			return false;
		}
		Integer tStatus = Integer.valueOf(String.valueOf(status));
		if (tStatus.equals(1) || tStatus.equals(2) || tStatus.equals(3) || tStatus.equals(4) || tStatus.equals(5)) {
			// 0：正常 1：延误 2：生产中 3：将结束 4：完成 5：终止;6:等待删除状态
			return true;
		}
		return false;
	}

	@Override
	public List<ControlInvoice> findFullByMin(Long lineId, String lineName, Date minDate, Long minProdId) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String recordTableName = ZhclUtil.getRecordTableName(line, "N", "B");
		//	查询数据
		String querySql = "SELECT PCN conName,CLN clientName,CLN builder,PCA address,TWP site,TG strengthName,TS slumps,TOt tech,TWS castModel,KV concreteStere,TCd carCode,Dpter dispatcher,Optor mixer,ANote proTime,BNo prodId,\n" +
			"\tBNo controlInvoiceNo,PPC indentCode,KV proStere,OBNo erpId,? lineId,? lineName,CASE WHEN OBNo is null THEN 1 ELSE 0 END conAuto\n" +
			"  FROM " + recordTableName + " where 1 = 1 ";
		if (minProdId != null) {
			querySql += "and BNo > " + minProdId;
		} else if (minDate != null) {
			querySql += "and ANote > '" + DateUtil.convertString(minDate, "yyyy-MM-dd HH:mm:ss") + "'";
		}
		querySql += " order by ANote;";
		Object[] params = {lineId, lineName};
		List<ControlInvoice> weightSpotChecks = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return weightSpotChecks;
	}

	@Override
	public List<ControlInvoice> findByTime(Long lineId, String lineName, String startTime, String endTime) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String recordTableName = ZhclUtil.getRecordTableName(line, "N", "B");
		//	查询数据
		String querySql = "SELECT PCN conName,CLN clientName,CLN builder,PCA address,TWP site,TG strengthName,TS slumps,TOt tech,TWS castModel,KV concreteStere,TCd carCode,Dpter dispatcher,Optor mixer,ANote proTime,BNo prodId,\n" +
			"\tBNo controlInvoiceNo,PPC indentCode,KV proStere,OBNo erpId,? lineId,? lineName,CASE WHEN OBNo is null THEN 1 ELSE 0 END conAuto\n" +
			"  FROM " + recordTableName + " where ANote >= ? and ANote < ? order by ANote";
		Object[] params = {lineId, lineName, startTime, endTime};
		List<ControlInvoice> weightSpotChecks = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return weightSpotChecks;
	}


}
